using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace etcetera.Mvc.UI.Html
{
    public static class DropDownListExtensions
    {
        /// <summary>
        /// Returns a select tag used to select a single option from a set of possible grouped choices.
        /// </summary>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="name">The name of the form field and used as a key to look up possible options. If ViewData[name] is a OptionGroupSelectList</param>
        /// <returns></returns>
        public static DropDownListEx<T> DropDownListEx<T>(this HtmlHelper htmlHelper, string name)
        {
            return DropDownListEx<T>(htmlHelper, name, name, null);
        }

        /// <summary>
        /// Returns a select tag used to select a single option from a set of possible grouped choices.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="name">The name of the form field and used as a key to look up possible options. If ViewData[name] is a OptionGroupSelectList</param>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        /// <returns></returns>
        public static DropDownListEx<T> DropDownListEx<T>(this HtmlHelper htmlHelper, string name, IDictionary<string, object> htmlAttributes)
        {
            return DropDownListEx<T>(htmlHelper, name, htmlAttributes);
        }

        /// <summary>
        /// Returns a select tag used to select a single option from a set of possible grouped choices.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="name">The name of the form field</param>
        /// <param name="optionGroupSelectListViewDataName">Name of the option group select list view data key.</param>
        /// <returns></returns>
        public static DropDownListEx<T> DropDownListEx<T>(this HtmlHelper htmlHelper, string name, string optionGroupSelectListViewDataName)
        {
            return DropDownListEx<T>(htmlHelper, name, optionGroupSelectListViewDataName, null);
        }

        /// <summary>
        /// Returns a select tag used to select a single option from a set of possible grouped choices.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="name">The name.</param>
        /// <param name="optionGroupSelectList">The option group select list.</param>
        /// <param name="optionLabel">Provides the text for a default empty valued option, if it is not null.</param>
        /// <returns></returns>
        public static DropDownListEx<T> DropDownListEx<T>(this HtmlHelper htmlHelper, string name, OptionGroupSelectList<T> optionGroupSelectList, string optionLabel)
        {
            return new DropDownListEx<T>(name, optionGroupSelectList, optionLabel);
        }

        /// <summary>
        /// Returns a select tag used to select a single option from a set of possible grouped choices.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="name">The name.</param>
        /// <param name="optionGroupSelectList">The option group select list.</param>
        /// <param name="optionLabel">Provides the text for a default empty valued option, if it is not null.</param>
        /// <param name="htmlAttributes">The HTML attributes.</param>
        /// <returns></returns>
        public static DropDownListEx<T> DropDownListEx<T>(this HtmlHelper htmlHelper, string name, OptionGroupSelectList<T> optionGroupSelectList, string optionLabel, IDictionary<string, object> htmlAttributes)
        {
            return new DropDownListEx<T>(name, optionGroupSelectList, optionLabel, htmlAttributes);
        }

        /// <summary>
        /// Returns a select tag used to select a single option from a set of possible grouped choices.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="htmlHelper">The HTML helper.</param>
        /// <param name="name">The name of the form field and used as a key to look up possible options. If ViewData[name] is a OptionGroupSelectList</param>
        /// <param name="optionGroupSelectListViewDataName">Name of the option group select list view data key.</param>
        /// <param name="optionLabel">Provides the text for a default empty valued option, if it is not null.</param>
        /// <returns></returns>
        public static DropDownListEx<T> DropDownListEx<T>(this HtmlHelper htmlHelper, string name, string optionGroupSelectListViewDataName, string optionLabel)
        {
            var optionGroupSelectList = htmlHelper.ViewData[optionGroupSelectListViewDataName];

            if (optionGroupSelectList is OptionGroupSelectList<T>)
                return new DropDownListEx<T>(name, optionGroupSelectList as OptionGroupSelectList<T>, optionLabel);

            throw new ArgumentException(string.Format("The parameter optionGroupListViewDataName is not a OptionGroupSelectList<{0}>", typeof(T)));
        }
    }
}